package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.able.UserAbleData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserAbleDataDaoImpl extends TempDataObjectDao<UserAbleData> {

    public static String getUserAbleDataSql = "select * from `t_u_able_data`";

    public Map<Long, Map<Integer, UserAbleData>> getUserAbleDataMap() {
        Map<Long, Map<Integer, UserAbleData>> mapMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserAbleDataSql);
            rs = call.executeQuery();
            while (rs.next()) {
                UserAbleData ableData = parse(rs);
                if (!mapMap.containsKey(ableData.getUserId())) {
                    mapMap.put(ableData.getUserId(), new ConcurrentHashMap<>());
                }
                mapMap.get(ableData.getUserId()).put(ableData.getActivityId(), ableData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return mapMap;
    }

    public static String updateUserAbleDataSql = "update `t_u_able_data` set ableItemId=?,ableTime=? where userId=? and activityId=?";

    @Override
    protected boolean updateData(UserAbleData userAbleData) {
        userAbleData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userAbleData.getAbleItemId()));
        para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(userAbleData.getAbleTime().getTime())));
        para.put(i++, new DbParameter(Types.BIGINT, userAbleData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userAbleData.getActivityId()));
        if (getDb().executeNoneQuery(updateUserAbleDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserAbleDataSql = "insert into `t_u_able_data`(userId,activityId,ableItemId,ableTime) values (?,?,?,?)";

    @Override
    protected boolean addData(UserAbleData userAbleData) {
        userAbleData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userAbleData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userAbleData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, userAbleData.getAbleItemId()));
        para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(userAbleData.getAbleTime().getTime())));
        if (getDb().executeNoneQuery(insertUserAbleDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserAbleData parseData(ResultSet rs) throws SQLException {
        UserAbleData userAbleData = new UserAbleData();
        userAbleData.setUserId(rs.getLong("userId"));
        userAbleData.setActivityId(rs.getInt("activityId"));
        userAbleData.setAbleItemId(rs.getInt("ableItemId"));
        userAbleData.setAbleTime(rs.getTimestamp("ableTime"));
        return userAbleData;
    }

}
